package no.ntnu.fp.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import no.ntnu.fp.su.Comment;
import no.ntnu.fp.su.Log;
import no.ntnu.fp.su.LogEntry;

/**
 * 
 * Basic database connector for the FP application
 * 
 * @author abs
 * 
 */
public class Database {

	Connection con;
	boolean connected = false;

	/**
	 * testing only
	 * 
	 * @param argsl
	 */
	public static void main(String args[]) {
		Database db = new Database();
		db.connect();
		//		
		// db.execute("insert into platform values(2,\"mannsverk\")");
		// db.execute("insert into welldata values(2,9,8,6,3,6,1,2,3)");
		// db.execute("insert into welldata values(3,5,2,3,1,8,2,2,4)");
		// db.execute("insert into platformwell values(1,2)");
		// db.execute("insert into platformwell values(2,3)");

		Log l = new Log(db,1);
		// LogEntry le = new LogEntry();
		// le.setWellID("1");
		//
		// le.setR(200.4);
		// le.setDeltaR(5.4);
		// le.setT(140.4);
		// le.setP(205.4);
		//		
		// l.addEntry(le);
		// l.writeToDb(le);
		long a = System.currentTimeMillis();
		l.readLogFromDb(60);
		System.out.println("readlog took " + (System.currentTimeMillis() - a)
				+ " milliseconds");
		System.out.println(l.getLog().size());
		for (LogEntry le : l.getLog()) {
			System.out.println("Well " + le.getWellID() + " "
					+ le.getTime().toString());
		}
		LogEntry le = l.getLog().get(2);
//		le.loadCommentsFromDB();
		// le.newComment("sweet shit");
		// le.newComment("weeee");
//		le.flushComments();
//		le.loadCommentsFromDB();
		for (Comment c : le.getComments()) {
			System.out.println(c.getCommentID());
		}
		// le.deleteComment(6);
		// le.deleteComment(7);

		// db.disconnect();
		System.out.println("done");
	}

	public Database() {
	}

	/**
	 * 
	 * Connects to the fp DB
	 * 
	 */
	public void connect() {
//		System.out.println("connecting to mysql db");
		String dbUrl = "jdbc:mysql://mysql.stud.ntnu.no/andrskom_fp";
		String user = "andrskom_fp";
		String pwd = "st0y";

		String dbClass = "com.mysql.jdbc.Driver";

		try {

			Class.forName(dbClass);
			con = DriverManager.getConnection(dbUrl, user, pwd);
			connected = true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			connected = false;
		}

		catch (SQLException e) {
			e.printStackTrace();
			connected = false;
		}
	}

	/**
	 * 
	 * Disconnects
	 * 
	 */
	public void disconnect() {
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		connected = false;
	}

	/**
	 * 
	 * Query the database for a ResultSet
	 * 
	 * @param query
	 *            String - the query for the database
	 * @return {@link ResultSet}
	 * 
	 * @throws NullPointerException
	 *             if not connected
	 */
	public ResultSet query(String query) {
		if (!connected)
			throw new NullPointerException("not connected to DB");
//		long a = System.currentTimeMillis();
		Statement stmt;
		try {
			stmt = con.createStatement();

			ResultSet r = stmt.executeQuery(query);
//			System.out.println("db query took "
//					+ (System.currentTimeMillis() - a) + " milliseconds");
			return r;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * Execute modifying statements
	 * 
	 * @param query
	 * @throws NullPointerException
	 *             if not connected
	 */
	public void execute(String query) {
		if (!connected)
			throw new NullPointerException("not connected to DB");
//		long a = System.currentTimeMillis();
		Statement stmt;
		try {
			stmt = con.createStatement();
			stmt.execute(query);
//			System.out.println("db execution took "
//					+ (System.currentTimeMillis() - a) + " milliseconds");
			return;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
